package com.xbk.seckill.stock.infrastructure.repository;

import cn.hutool.core.collection.CollectionUtil;
import com.xbk.seckill.common.model.enums.SeckillStockBucketStatus;
import com.xbk.seckill.stock.domain.model.entity.SeckillStockBucket;
import com.xbk.seckill.stock.domain.repository.SeckillStockBucketRepository;
import com.xbk.seckill.stock.infrastructure.mapper.SeckillStockBucketMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 分桶库存Repository实现类
 *
 * @author xiexu
 * @date 2025/3/8 21:12
 */
@Slf4j
@Component
public class SeckillStockBucketRepositoryImpl implements SeckillStockBucketRepository {

    @Autowired
    private SeckillStockBucketMapper seckillStockBucketMapper;

    /**
     * 批量提交商品库存信息
     *
     * @param goodsId 商品id
     * @param buckets 分桶列表
     * @return
     */
    @Override
    public boolean submitBuckets(Long goodsId, List<SeckillStockBucket> buckets) {
        if (goodsId == null || CollectionUtil.isEmpty(buckets)) {
            return false;
        }
        // 删除原有分桶数据
        seckillStockBucketMapper.deleteByGoodsId(goodsId);
        // 保存新的分桶数据
        seckillStockBucketMapper.insertBatch(buckets);
        return true;
    }

    @Override
    public boolean increaseStock(Integer quantity, Integer serialNo, Long goodsId) {
        if (quantity == null || serialNo == null || goodsId == null) {
            return false;
        }
        seckillStockBucketMapper.increaseStock(quantity, serialNo, goodsId);
        return true;
    }

    /**
     * 扣减数据库商品库存
     */
    @Override
    public boolean decreaseStock(Integer quantity, Integer serialNo, Long goodsId) {
        if (quantity == null || serialNo == null || goodsId == null) {
            return false;
        }
        seckillStockBucketMapper.decreaseStock(quantity, serialNo, goodsId);
        return true;
    }

    /**
     * 根据商品id从数据库中获取库存分桶列表
     *
     * @param goodsId
     * @return
     */
    @Override
    public List<SeckillStockBucket> getBucketsByGoodsId(Long goodsId) {
        if (Objects.isNull(goodsId)) {
            return Collections.emptyList();
        }
        // 根据商品id从数据库中获取库存分桶列表
        return seckillStockBucketMapper.getBucketsByGoodsId(goodsId);
    }

    /**
     * 关闭库存分桶
     *
     * @param goodsId 商品id
     * @return
     */
    @Override
    public boolean suspendBuckets(Long goodsId) {
        if (Objects.isNull(goodsId)) {
            return false;
        }
        seckillStockBucketMapper.updateStatusByGoodsId(SeckillStockBucketStatus.DISABLED.getCode(), goodsId);
        return true;
    }

    /**
     * 恢复库存分桶
     *
     * @param goodsId
     * @return
     */
    @Override
    public boolean resumeBuckets(Long goodsId) {
        if (goodsId == null) {
            return false;
        }
        seckillStockBucketMapper.updateStatusByGoodsId(SeckillStockBucketStatus.ENABLED.getCode(), goodsId);
        return true;
    }

}
